package com.yanqu.road.dao.impl.curio;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.curio.UserCurioCountData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserCurioCountDataDaoImpl extends TempDataObjectDao<UserCurioCountData> {

    public static String getUserCurioCountDataSql = "select * from `t_u_curio_count_data` where `userId`=?";
    public Map<Integer, Map<Integer, UserCurioCountData>> getUserCurioCountData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UserCurioCountData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserCurioCountDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCurioCountData data = parse(rs);
                if(!map.containsKey(data.getType())){
                    map.put(data.getType(), new ConcurrentHashMap<>());
                }
                map.get(data.getType()).put(data.getChildType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserCurioCountDataSql = "update `t_u_curio_count_data` set countValue=? where userId=? and type=? and childType=?";
    @Override
    protected boolean updateData(UserCurioCountData userCurioCountData) {
        userCurioCountData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCurioCountData.getCountValue()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioCountData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioCountData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioCountData.getChildType()));
        if (getDb().executeNoneQuery(updateUserCurioCountDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserCurioCountDataSql = "insert into `t_u_curio_count_data`(userId,type,childType,countValue) values (?,?,?,?)";
    @Override
    protected boolean addData(UserCurioCountData userCurioCountData) {
        userCurioCountData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCurioCountData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioCountData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioCountData.getChildType()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioCountData.getCountValue()));
        if (getDb().executeNoneQuery(insertUserCurioCountDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserCurioCountData parseData(ResultSet rs) throws SQLException {
        UserCurioCountData userCurioCountData = new UserCurioCountData();
        userCurioCountData.setUserId(rs.getLong("userId"));
        userCurioCountData.setType(rs.getInt("type"));
        userCurioCountData.setChildType(rs.getInt("childType"));
        userCurioCountData.setCountValue(rs.getLong("countValue"));

        return userCurioCountData;
    }

}
